﻿<%@ Page Title="" Language="C#" MasterPageFile="../../Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

<% if (false)
   { %>
<script src="jquery-vsdoc.js" type="text/javascript"></script>
<script src="jquery-ui-1.8.custom.min.js" type="text/javascript"></script>
<script src="jquery.form.js" type="text/javascript"></script>
<%} %>


<%
    var allProjects = (IList<ProjectViewModel>)ViewData["AllProejcts"];
    var PlaneTypes = (IList<PlaneType>)ViewData["PlaneTypes"];
%>

<script type="text/javascript">
    var isCreatingPlaneTask = false;
    var URL_GetTeamMembers = "<%= Url.EzDeskUrl("Project", "/GetTeamMembers", "ajax") %>";
    var URL_GetPlaneTaskItem = "<%= Url.EzDeskUrl("Project", "/PlaneTaskItemInList", "ajax") %>";


    $(function () {

        var projectTitles = $('#projectList dt');
        projectTitles
        .css({
            'cursor':'pointer',
            'background-color': '#f7f7f7',
            'border':'1px solid #dfdfdf',
            'display':'block',
            'padding': '.3em'
            })
        .click(function () {
            var body = $(this).next();
            body.toggle();
        })
        .next()
          .hide()

        $('#createPlaneTaskForm').ajaxForm();
        $('#planeStartOn').datepicker();
        $('#planeEndOn').datepicker();


        // 创建新阶段 按钮点击事件
        $('.btnCreatePlane').click(function () {
            if (!isCreatingPlaneTask) {
                var thisBtn = $(this);

                var teamIdStr = $('.projectTeamId', thisBtn.parent()).val();
                var projectIdStr = $('.projectId', thisBtn.parent()).val();

                $.ajaxBusy('begin', this, thisBtn, false);
            
                // todo: 清空form中数据
                document.getElementById('createPlaneTaskForm').reset();

                // 在form中加入projectId数据
                $('#p_projectId').remove();
                $('#createPlaneTaskForm')                
                .append($('<input id="p_projectId" name="p_projectId" type="hidden"/>').val(projectIdStr));
                
                // 在form中绑定项目成员
                // 得到成员 
                $('#cmbDirectors').empty();
                // ajax去获得团队所有成员 TODO:这地方考虑缓存一下 
                $.getJSON(URL_GetTeamMembers, { teamId : teamIdStr }, function (e) {
                    // 绑数据
                    $(e.Members).each(function (index, item) { 
                        $('<option/>').val(item.Id).html(item.Name).appendTo('#cmbDirectors');
                    });

                    $.ajaxBusy('end');

                    thisBtn.slideUp();
                    $('#panelCreatePlane')    
          
                    .appendTo(thisBtn.parent())
                    .show();

                    isCreatingPlaneTask = true;

                });


            }else{
                alert('先关闭已经打开的对话框');
            }
        });

        // 关闭 创建新阶段 对话框
        $('#btnCancleCreatePlane').click(function () {
            // 收起和清理对话框本身 显示 a标签
            $('#panelCreatePlane').slideUp();
            $('#panelCreatePlane').parent().find('.btnCreatePlane').show(); // a标签
            isCreatingPlaneTask = false;
        });

        $('#btnOkCreatePlane').click(function () {
            $.ajaxBusy('begin', this);
            // 提交添加新阶段的Form
            $('#createPlaneTaskForm').ajaxSubmit(createPlaneSubmitCallbak);
            
            // 收起对话框本身 显示 a标签
            $('#panelCreatePlane').slideUp();
            $('#panelCreatePlane').parent().find('.btnCreatePlane').show(); // a标签
            isCreatingPlaneTask = false;
        });
    })

    function createPlaneSubmitCallbak(e){
        $.displayInfo(e.State, e.Info);
        
        // 将更新的PlaneTaskItem加入列表
        var newPlaneTaskId = e.Data.Id;
        var projectId = e.Data.ProjectId;

        $.get(URL_GetPlaneTaskItem, { planeTaskId : newPlaneTaskId }, function (e) {
            var pnlPlanes = $('#planes-' + projectId);
            $('.no-planes', pnlPlanes).remove();
            $(pnlPlanes).append($(e));
            $(e).fadeIn();
            $.ajaxBusy('end');
        }); 


    }
</script>
                <div id="panelCreatePlane" class="hide" style="background: #f7f7f7; border: solid #bcbcbc; border-width: 1px 2px 2px; width: 350px; margin: 5px; padding: 0 10px 0 10px">
                    <form id="createPlaneTaskForm" action="/Ajax/Project/CreatePlaneTask" method="post">
                        <h2>添加阶段任务</h2>
                        <div>
                            <label for="cmbPlaneType">选择项目阶段：</label>
                            <select id="cmbPlaneType" name="p_planeType">
                                <%foreach(var planeType in PlaneTypes){ %>
                                <option id="<%= planeType.Name %>"><%= planeType.Name %></option>
                                <%} %>
                            </select><br />

                            <label for="cmbDirectors">选择阶段负责人：</label>
                            <select id="cmbDirectors" name="p_directorId">
                                
                            </select><br />

                            <label for="planeStartOn">开始时间：</label>
                            <input type="text" id="planeStartOn" name="p_startOn" /><br />                        
                            <label for="planeEndOn">结束时间：</label>
                            <input type="text" id="planeEndOn" name="p_endOn" /><br />
                        </div>
                        <div style="float: right; margin: 10px;">
                            <input id="btnOkCreatePlane" type="button" value="确定" /><input id="btnCancleCreatePlane" type="button" value="取消" />
                        </div>
                        <div style="clear: both;"></div>
                    </form>
                </div>


    <div style="width: 800px;">
        <h2>项目列表</h2>
        
        <dl id="projectList">
        <% foreach (var project in allProjects)
           {%>
           <% if (!project.BasicInfo.IsCloesd)
              {%>

            <dt class="ui-state-default" style="margin: 10px 0 0 0">
                <table><tr>
                <td>
                    <%: project.BasicInfo.ProjectName %>(<%: project.BasicInfo.ProjectNo%>)</td>
                    <td><label for="lblProjectType">类型: </label><label id="lblProjectType"><%: project.BasicInfo.ProjectType.ToString() %></label></td>
                    <td><label for="lblCreateOn">创建时间: </label><label id="lblCreateOn"><%: project.BasicInfo.CreateOn.FormattedFullDate()%></label></td>
                    </tr>
                </table>

                
            </dt>
            <dd style="border: 1px solid #dfdfdf; padding: 5px;">
                <div class="sec">
                    <label for="lblCreateOn">任务完成情况: </label><label id="Label1">20/80</label>
                </div>
                <div class="sec"><a href="<%= Url.EzDeskUrl("Project", "/ProjectDetail?projectId=" + project.BasicInfo.Id.ToString()) %>")>项目任务管理</a></div>

                <div id="planes-<%: project.BasicInfo.Id %>" style=" width: 500px; border: 1px solid #dfdfdf; padding: 5px;">
                    <div><b>阶段任务</b></div>
                    <% if(project.PlaneTasks.Count == 0){ %>
                        <div class="no-planes">还没有阶段任务</div>    
                    <%} else{
                          foreach (var planeTask in project.PlaneTasks)
                          {
                              Html.RenderPartial("_planeTaskItemInList", planeTask);
                          } %>
                    <%} %>

                    <input type="hidden" class="projectTeamId" value="<%: project.BasicInfo.ProjectTeamId %>" />
                    <input type="hidden" class="projectId" value="<%: project.BasicInfo.Id %>" />
                    <a class="btnCreatePlane" href="#">创建新的阶段任务</a>
                </div>
                               
            </dd>        

           <%}else{ %>
           <dt class="ui-state-disabled">
                <span>
                    <%: project.BasicInfo.ProjectName %>(<%: project.BasicInfo.ProjectNo%>)
                </span>
                <span style="float: right; ">
                    已关闭
                </span>
            </dt>
            <dd>
                
            </dd>  


           <%} %>

      

        <%} %>

        </dl>
    
    </div>

</asp:Content>
